/*
 * JsonTowerPanel.java
 *
 * Created on Aug 20, 2009, 10:19:32 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.map;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class JsonTowerOptionsPanel extends javax.swing.JPanel {

	private JsonTowerOptions to;

	/** Creates new form JsonTowerPanel */
	public JsonTowerOptionsPanel(JsonTowerOptions tower) {
		initComponents();
		to = tower;
		jLabelName.setText(to.toString());
		DefaultComboBoxModel model = new DefaultComboBoxModel(JsonTowerOptions.optionKey.values());
		jComboBoxSettings.setModel(model);
		DefaultTableModel tmodel = (DefaultTableModel) jTableSettings.getModel();
		if (to.options != null) {
			for (Entry<String, String> e : to.options.entrySet()) {
				Object[] rowData = {
					JsonTowerOptions.optionKey.valueOf(e.getKey()),
					e.getValue()
				};
				tmodel.addRow(rowData);
			}
		}
		tmodel.addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e) {
				readTable();
			}
		});
		invalidate();
		repaint();
	}

	private void deleteSelectedRow() {
		int selectedRow = jTableSettings.getSelectedRow();
		DefaultTableModel model = (DefaultTableModel) jTableSettings.getModel();
		model.removeRow(selectedRow);
		readTable(model.getDataVector());
	}

	private void addOption() {
		JsonTowerOptions.optionKey key = (JsonTowerOptions.optionKey) jComboBoxSettings.getSelectedItem();
		DefaultTableModel model = (DefaultTableModel) jTableSettings.getModel();
		Object[] rowData = {key, key.defaultValue};
		model.addRow(rowData);
		readTable(model.getDataVector());
	}

	private void setHelpSelectedOption() {
		int selectedRow = jTableSettings.getSelectedRow();
		DefaultTableModel model = (DefaultTableModel) jTableSettings.getModel();
		Object value = model.getValueAt(selectedRow, 0);
		JsonTowerOptions.optionKey key = JsonTowerOptions.optionKey.valueOf(value.toString());
		jTextAreaHelp.setText(key.description);
	}

	private void readTable() {
		DefaultTableModel model = (DefaultTableModel) jTableSettings.getModel();
		readTable(model.getDataVector());
	}

	private void readTable(Vector dataVector) {
		if (dataVector.size() == 0) {
			to.options = null;
			return;
		}
		if (to.options == null) {
			to.options = new HashMap<String, String>();
		}
		to.options.clear();
		for (Object row : dataVector) {
			if (row instanceof Vector) {
				Vector entry = (Vector) row;
				JsonTowerOptions.optionKey key = JsonTowerOptions.optionKey.valueOf(entry.get(0).toString());
				String value = entry.get(1).toString();
				to.options.put(key.name(), value);
			}
		}
		System.out.println("JTSP::readTable: read " + to.options.size() + " options.");
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabelName = new javax.swing.JLabel();
        jComboBoxSettings = new javax.swing.JComboBox();
        jButtonAddSetting = new javax.swing.JButton();
        jButtonDelSetting = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSettings = new javax.swing.JTable();
        jTextAreaHelp = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Selected Tower Type"));
        setLayout(new java.awt.GridBagLayout());

        jLabelName.setText("jLabel1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jLabelName, gridBagConstraints);

        jComboBoxSettings.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxSettings.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxSettingsItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.0010;
        add(jComboBoxSettings, gridBagConstraints);

        jButtonAddSetting.setText("+");
        jButtonAddSetting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddSettingActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        add(jButtonAddSetting, gridBagConstraints);

        jButtonDelSetting.setText("-");
        jButtonDelSetting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDelSettingActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        add(jButtonDelSetting, gridBagConstraints);

        jTableSettings.setAutoCreateRowSorter(true);
        jTableSettings.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Setting", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableSettings.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTableSettings.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTableSettingsFocusGained(evt);
            }
        });
        jTableSettings.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTableSettingsPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSettings);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.weighty = 0.01;
        add(jScrollPane1, gridBagConstraints);

        jTextAreaHelp.setLineWrap(true);
        jTextAreaHelp.setRows(2);
        jTextAreaHelp.setText("select an option for a description");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0E-4;
        add(jTextAreaHelp, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

	private void jButtonDelSettingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDelSettingActionPerformed
		deleteSelectedRow();
	}//GEN-LAST:event_jButtonDelSettingActionPerformed

	private void jButtonAddSettingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddSettingActionPerformed
		addOption();
	}//GEN-LAST:event_jButtonAddSettingActionPerformed

	private void jComboBoxSettingsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxSettingsItemStateChanged
		JsonTowerOptions.optionKey selectedItem = (JsonTowerOptions.optionKey) jComboBoxSettings.getSelectedItem();
		jTextAreaHelp.setText(selectedItem.description);
	}//GEN-LAST:event_jComboBoxSettingsItemStateChanged

	private void jTableSettingsPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jTableSettingsPropertyChange
		if (evt.getPropertyName().equalsIgnoreCase("tablecelleditor")) {
			setHelpSelectedOption();
		}
	}//GEN-LAST:event_jTableSettingsPropertyChange

	private void jTableSettingsFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTableSettingsFocusGained
		setHelpSelectedOption();
	}//GEN-LAST:event_jTableSettingsFocusGained
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddSetting;
    private javax.swing.JButton jButtonDelSetting;
    private javax.swing.JComboBox jComboBoxSettings;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableSettings;
    private javax.swing.JTextArea jTextAreaHelp;
    // End of variables declaration//GEN-END:variables
}
